package com.yakumosakura.yakumoblade.specialeffects.utils;

import com.yakumosakura.yakumoblade.utils.SlashBladeUtils;
import com.yakumosakura.yakumoblade.Yakumoblade;
import mods.flammpfeil.slashblade.capability.slashblade.ISlashBladeState;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import mods.flammpfeil.slashblade.registry.specialeffects.SpecialEffect;
import mods.flammpfeil.slashblade.slasharts.SlashArts;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;


import static tfar.classicbar.ClassicBar.logger;

public class SeEX extends SpecialEffect {
    public SeEX(int questlevel) {
        super(questlevel);
    }


    public static Optional<ResourceLocation> gettext(Player playerIn){
        return playerIn.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE)
                .map(state -> state.getTexture()).get();
    }
    public static void settext(Player playerIn,String text){
        playerIn.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE)
                .ifPresent(state -> state.setTexture(Yakumoblade.prefix(text)));
    }
    public static SlashArts getsa(Player playerIn){
        return playerIn.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE)
                .map(state -> state.getSlashArts()).get();
    }
    public static void setsa(Player playerIn, ResourceLocation sa) {
        if (playerIn == null) {
            logger.warn("Player is null, cannot set slash arts key.");
            return;
        }

        ItemStack mainHandItem = playerIn.getMainHandItem();
        if (mainHandItem.isEmpty()) {
            logger.warn("Player's main hand item is empty, cannot set slash arts key.");
            return;
        }

        @NotNull LazyOptional<ISlashBladeState> capability = mainHandItem.getCapability(ItemSlashBlade.BLADESTATE);
        capability.ifPresent(state -> {
            try {
                state.setSlashArtsKey(sa);
            } catch (Exception e) {
                logger.error("Failed to set slash arts key", e);
            }
        });

        if (!capability.isPresent()) {
            logger.warn("Item does not have BLADESTATE capability, cannot set slash arts key.");
        }
    }
    public static String gettran(Player playerIn){
        return playerIn.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE)
                .map(state -> state.getTranslationKey()).get();
    }
    public static String gettransa(ResourceLocation sa){
        return "slash_art."+sa.toLanguageKey();
    }
    public static String createis(Player _player,ResourceLocation sa){
        return Component.translatable(gettran(_player)).getString()
                +Component.translatable("yakumoblade.types.open").getString()
                +Component.translatable(gettransa(sa)).getString();
    }


    public static boolean hasSpecialEffect(ItemStack stack, RegistryObject<SpecialEffect> effect) {
      if (SlashBladeUtils.hasSpecialEffect(stack, effect)){
          return true;
      }
        return false; // 没有找到指定的特殊效果
    }







}
